const indexRouter = require('koa-router')()

const {info , userNav} = require('./mock/services/user')
const {login , logout, smsCaptcha, twofactor} = require('./mock/services/auth')

const responseBody = {
  message: '',
  timestamp: 0,
  result: null,
  code: 0
}

const builder = (data, message, code = 0, headers = {}) => {
  responseBody.result = data
  if (message !== undefined && message !== null) {
    responseBody.message = message
  }
  if (code !== undefined && code !== 0) {
    responseBody.code = code
    responseBody._status = code
  }
  if (headers !== null && typeof headers === 'object' && Object.keys(headers).length > 0) {
    responseBody._headers = headers
  }
  responseBody.timestamp = new Date().getTime()
  return responseBody
}

const userApi = {
  Login: '/auth/login',
  Logout: '/auth/logout',
  ForgePassword: '/auth/forge-password',
  Register: '/auth/register',
  twoStepCode: '/auth/2step-code',
  SendSms: '/account/sms',
  SendSmsErr: '/account/sms_err',
  // get my info
  UserInfo: '/user/info',
  UserMenu: '/user/nav'
}

indexRouter.post(userApi.Login, async (ctx, next) => {
  const params = ctx.request.body
  ctx.body = login(params)
})

indexRouter.get(userApi.UserMenu, async (ctx, next) => {
  ctx.body = userNav(ctx.query)
})

indexRouter.post(userApi.SendSms, async (ctx, next) => {
  const params = ctx.request.body
  ctx.body = smsCaptcha(params)
})

indexRouter.get(userApi.UserInfo, async (ctx, next) => {
  const params = ctx.request.body
  ctx.body = info(params)
})

indexRouter.post(userApi.Logout, async (ctx, next) => {
  const params = ctx.request.body
  ctx.body = logout(params)
})


// /**
//  * get user 2step code open?
//  * @param parameter {*}
//  */
// export function get2step (parameter) {
//   return request({
//     url: userApi.twoStepCode,
//     method: 'post',
//     data: parameter
//   })
// }


module.exports = indexRouter
// export default indexRouter

